<template>
  <Modal v-model="isShow"
         footer-hide
         @on-cancel="closeEvent"
         :mask-closable="false"
         :title="title">
    <Form v-if="isShow" ref="form" :label-width="100" :model="formData" :rules="rules">
      <FormItem label="字典编码" prop="dictCode">
        <Input readonly v-model="formData.dictCode"/>
      </FormItem>
      <FormItem label="子字典编码" prop="dictCode">
        <Input readonly v-model="formData.childDictCode"/>
      </FormItem>
      <FormItem label="字典值" prop="dictDataCode">
        <Select ref="select" v-model="formData.dictDataCode">
          <Option v-for="item in dictDatas" :value="item.code" :key="item.code" :label="item.name">
            {{ item.name }}
          </Option>
        </Select>
      </FormItem>
      <FormItem label="名称" prop="name">
        <Input v-model="formData.name"/>
      </FormItem>
      <FormItem label="排序值" prop="orderValue">
        <Input type="number" number v-model="formData.orderValue"/>
      </FormItem>
      <FormItem label="状态" prop="status">
        <DictSelect v-model="formData.status" dictCode="YesNo:EnableDisable"/>
      </FormItem>
      <FormItem label="备注" prop="remark">
        <Input type="textarea" maxlength="1024" show-word-limit :autosize="{minRows: 3}" v-model="formData.remark"/>
      </FormItem>
      <FormItem class="form-bottom-btn">
        <Button @click="submitBtnClick" type="primary">保存</Button>
      </FormItem>
    </Form>
  </Modal>
</template>

<script>
import {Message} from "view-ui-plus";
import {saveOrUpdateChildDict} from "@/api/sys/childDictData";
import {RESPONSE_CODE_SUCCESS} from "@/constant/response";
import {getDictMap} from "@/util";

const formData = {
  id: undefined,
  dictCode: '',
  childDictCode: '',
  dictDataCode: '',
  name: '',
  orderValue: 0,
  status: 1,
  remark: ''
}

export default {
  name: "SysChildDictDataEdit",
  data() {
    return {
      isShow: false,
      title: "",
      dictDatas: [],
      formData: formData,
      rules: {
        dictCode: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        childDictCode: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        dictDataCode: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        orderValue: [
          {required: true, type: 'number', message: '不能为空', trigger: 'blur'},
        ],
        status: [
          {required: true, type: 'number', message: '不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    show({title, data}) {
      this.title = title
      this.formData = this.$utils.clone(formData)
      if (data) {
        Object.assign(this.formData, data)
      }
      getDictMap(this.formData.dictCode).then(res => {
        let dict = res.get(this.formData.dictCode)
        for (let item of dict.datas) {
          item.code = item.code + ""
        }
        this.dictDatas = dict.datas;
      })
      this.isShow = true
    },
    closeEvent() {
      this.formData = this.$utils.clone(formData)
    },
    submitBtnClick() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        saveOrUpdateChildDict(this.formData).then(res => {
          if (res.code == RESPONSE_CODE_SUCCESS) {
            this.$emit("ok")
            Message.success("保存成功")
            this.isShow = false
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
